package chessengine;

import chesspieces.Bishop;
import chesspieces.King;
import chesspieces.Knight;
import chesspieces.Pawn;
import chesspieces.Queen;
import chesspieces.Rook;
import java.util.*;

public class ChessEngine {

    public ChessEngine() {
    }
    static ChessBoard cb;

    public static void Initialize() {
        Pawn[] Ppawns = new Pawn[8];
        Pawn[] Epawns = new Pawn[8];
        for (int i = 0; i < 8; i++) {
            Ppawns[i] = new Pawn(i, 1, true, cb);
        }
        for (int i = 0; i < 8; i++) {
            Epawns[i] = new Pawn(i, 6, false, cb);
        }
        for (int i = 0; i < 8; i++) {
            cb.pieces[i] = Ppawns[i];
        }
        cb.pieces[8] = new Rook(0, 0, true, cb);
        cb.pieces[9] = new Knight(1, 0, true, cb);
        cb.pieces[10] = new Bishop(2, 0, true, cb);
        cb.pieces[11] = new Queen(3, 0, true, cb);
        cb.pieces[12] = new King(4, 0, true, cb);
        cb.pieces[13] = new Bishop(5, 0, true, cb);
        cb.pieces[14] = new Knight(6, 0, true, cb);
        cb.pieces[15] = new Rook(7, 0, true, cb);

        for (int i = 0; i < 8; i++) {
            cb.pieces[i + 16] = Epawns[i];
        }
        cb.pieces[24] = new Rook(0, 7, false, cb);
        cb.pieces[25] = new Knight(1, 7, false, cb);
        cb.pieces[26] = new Bishop(2, 7, false, cb);
        cb.pieces[27] = new Queen(3, 7, false, cb);
        cb.pieces[28] = new King(4, 7, false, cb);
        cb.pieces[29] = new Bishop(5, 7, false, cb);
        cb.pieces[30] = new Knight(6, 7, false, cb);
        cb.pieces[31] = new Rook(7, 7, !true, cb);
    }

    public static void main(String[] args) {
        Scanner Input = new Scanner(System.in);
        cb = new ChessBoard();
        Initialize();
        cb.Initialize(cb.pieces);
        System.out.println(cb);
        boolean player1 = true;
        while (true) {
            if (player1) {
                String p = Input.next();
                String m = Input.next();
                int[] move = Interpreter.translate(m);
                int[] pos = Interpreter.translate(p);

                if (cb.getPieceAt(pos[0], pos[1]).move(move[0], move[1], player1)) {
                    cb.Update();
                    player1 = !player1;
                    System.out.println(cb);
                }
            } else {

                String p = Input.next();
                String m = Input.next();
                int[] move = Interpreter.translate(m);
                int[] pos = Interpreter.translate(p);

                if (cb.getPieceAt(pos[0], pos[1]).move(move[0], move[1], player1)) {
                    cb.Update();
                    player1 = !player1;
                    System.out.println(cb);
                }
            }
        }

    }
}
